
let api = require("../../config/api");
let utils = require("../../utils/util");

// pages/login/login.js
Page({
    countDownTime:0,
    timeNum:60,
    timer:{},

    /**
     * 页面的初始数据
     */
    data: {
        smsCodeEnable:true,
        smsCodeBtnTitle:"获取",
        phone:"",
        smsCode:"",
    
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.countDownTime = this.timeNum;
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    onGetSMSCode(){
        
        if (this.data.phone.length == 0){
            wx.showToast({
              title: '手机号码不能为空',
              icon:"none"
            })
            return;
        }

        if (this.data.phone.length != 11){
            wx.showToast({
                title: '手机号格式不正确',
                icon:"none"
              })
              return;
        }

        if (this.countDownTime == this.timeNum){
            this.setData({smsCodeEnable:false});
            this.countTime();

            wx.showLoading();
            utils.request(api.sendSMS, {"mobile":this.data.phone},'POST',{'Content-Type':'application/json'}).then(function (res){
                wx.hideLoading();
                if (res.code == 0){
                    
                }else{
                    wx.showToast({
                      title: res.message,
                      icon:"none"
                    })
                }
            }).catch(function (e){
                wx.hideLoading();
                wx.showToast({
                  title: e,
                  icon:"none"
                })                
            });
        }else{
            
        }
    },
    countTime(){
        let that = this;
        that.timer = setInterval(function (){
            that.countDownTime--;
            if (that.countDownTime < 2){
                clearInterval(that.timer);
                that.setData({smsCodeEnable:true,smsCodeBtnTitle:"获取"});
            }else{
                that.setData({smsCodeEnable:false,smsCodeBtnTitle:that.countDownTime + "s"});
            }
        },1000)
        
    },
    onPhoneInput(e){
        let phone = e.detail.value;
        this.setData({phone:phone});
    },
    onSMSCodeInput(e){
        let smsCode = e.detail.value;
        this.setData({smsCode:smsCode});
    },
    onBindPhone(){
        if (this.data.smsCode.length == 0){
            wx.showToast({
              title: '验证码不能为空',
              icon:"none"
            })
            return;
        }

        wx.showLoading();
        utils.request(api.verifySMS,{"mobile":this.data.phone,"sms_code":this.data.smsCode},"POST",{"Content-Type":"application/json"}).then(function (res){
            wx.hideLoading();
            if (res.code == 0){
                wx.setStorageSync('bindPhone',true);

                wx.showToast({
                  title: '绑定成功',
                  icon:"none",
                  complete:function (){
                    setTimeout(() => {
                        wx.navigateBack();
                    }, 1000);
                  }
                })

            }else{
                wx.showToast({
                  title: res.message,
                  icon:"none"
                })
            }
        }).catch(function (e){
            wx.hideLoading();
            wx.showToast({
              title: e,
              icon:"none"
            })
        });
        
    }
})